package com.leoman.comment.controller;

import com.leoman.comment.entity.Comment;
import com.leoman.comment.service.WorkCommentService;
import com.leoman.comment.service.impl.CommentServiceImpl;
import com.leoman.common.controller.common.GenericEntityController;
import com.leoman.common.factory.DataTableFactory;
import com.leoman.entity.Constant;
import com.leoman.exception.GeneralExceptionHandler;
import com.leoman.user.entity.UserInfo;
import com.leoman.user.service.UserService;
import com.leoman.utils.JsonUtil;
import com.leoman.utils.WebUtil;
import com.leoman.work.entity.*;
import com.leoman.work.service.WorkComicService;
import com.leoman.work.service.WorkNovelService;
import com.leoman.work.service.WorkService;
import com.leoman.work.service.WorkVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 *
 * Created by 蔡琦 on 2016/6/22 0022.
 */
@Controller
@RequestMapping(value = "admin/comment")
public class CommentController extends GenericEntityController<Comment, Comment, CommentServiceImpl> {

    @Autowired
    private WorkCommentService workCommentService;

    @Autowired
    private WorkVideoService workVideoService;

    @Autowired
    private WorkComicService workComicService;

    @Autowired
    private WorkNovelService workNovelService;

    @Autowired
    private WorkService workService;

    @Autowired
    private UserService userService;

    /**
     * 列表页面
     *
     * @return path
     */
    @RequestMapping(value = "/index")
    public String index() {
        return "comment/list";
    }

    /**
     * 查询列表
     */
    @RequestMapping(value = "/list")
    public void list(HttpServletResponse response,
                     String name,
                     String mobile,
                     Integer draw,
                     Integer start,
                     Integer length) {
        try {

            int pageNum = getPageNum(start, length);

            Page<WorkComment> page = workCommentService.pageList(name, mobile, Constant.CATEGORY_TYPE_004, pageNum, length);
            for (WorkComment workComment : page.getContent()) {

                if (workComment.getIsDetail() == Constant.COMMENT_PRAISE_ISDETAIL_NO) {

                    // 针对作品评论
                    Work work = workService.queryByPK(workComment.getWorkId());
                    workComment.setWork(work);
                }else if (workComment.getIsDetail() == Constant.COMMENT_PRAISE_ISDETAIL_YES) {

                    // 作品章节评论
                    WorkVideo workVideo = workVideoService.queryByPK(workComment.getWorkId());
                    workComment.setWorkVideo(workVideo);
                    workComment.setWork(workVideo.getWork());
                }

                /*UserInfo userInfo = userService.findOneByUserId(comment.getFromUser().getId());
                comment.setUserInfo(userInfo);*/
            }
            Map<String, Object> result = DataTableFactory.fitting(draw, page);
            WebUtil.print(response, result);
        } catch (Exception e) {
            GeneralExceptionHandler.log(e);
            WebUtil.print(response, emptyData);
        }
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    private Integer delete(Long commentId) {
        workCommentService.deleteComment(commentId);

        return 1;
    }

    /**
     * 列表页面
     *
     * @return path
     */
    @RequestMapping(value = "/indexNovel")
    public String indexNovel() {
        return "comment/listNovel";
    }

    /**
     * 查询列表
     */
    @RequestMapping(value = "/listNovel")
    public void listNovel(HttpServletResponse response,
                          String name,
                          String mobile,
                          Integer draw,
                          Integer start,
                          Integer length) {
        try {

            int pageNum = getPageNum(start, length);

            Page<WorkComment> page = workCommentService.pageList(name, mobile, Constant.CATEGORY_TYPE_005, pageNum, length);
            for (WorkComment comment : page.getContent()) {
                if (comment.getIsDetail() == Constant.COMMENT_PRAISE_ISDETAIL_NO) {

                    // 作品评论
                    Work work = workService.queryByPK(comment.getWorkId());
                    comment.setWork(work);
                }else if (comment.getIsDetail() == Constant.COMMENT_PRAISE_ISDETAIL_YES) {

                    // 小说章节评论
                    WorkNovel workNovel = workNovelService.queryByPK(comment.getWorkId());
                    comment.setWorkNovel(workNovel);
                    comment.setWork(workNovel.getWork());
                }
                /*UserInfo userInfo = userService.findOneByUserId(comment.getFromUser().getId());
                comment.setUserInfo(userInfo);*/
            }
            Map<String, Object> result = DataTableFactory.fitting(draw, page);
            WebUtil.print(response, result);
        } catch (Exception e) {
            GeneralExceptionHandler.log(e);
            WebUtil.print(response, emptyData);
        }
    }

    /**
     * 列表页面
     *
     * @return path
     */
    @RequestMapping(value = "/indexComic")
    public String indexComic() {
        return "comment/listComic";
    }

    /**
     * 查询列表
     */
    @RequestMapping(value = "/listComic")
    public void listComic(HttpServletResponse response,
                          String name,
                          String mobile,
                          Integer draw,
                          Integer start,
                          Integer length) {
        try {

            int pageNum = getPageNum(start, length);

            Page<WorkComment> page = workCommentService.pageList(name, mobile, Constant.CATEGORY_TYPE_006, pageNum, length);
            for (WorkComment comment : page.getContent()) {
                if (comment.getIsDetail() == Constant.COMMENT_PRAISE_ISDETAIL_NO) {

                    // 作品评论
                    Work work = workService.queryByPK(comment.getWorkId());
                    comment.setWork(work);
                }else if (comment.getIsDetail() == Constant.COMMENT_PRAISE_ISDETAIL_YES) {

                    // 漫画章节评论
                    WorkComic workComic = workComicService.queryByPK(comment.getWorkId());
                    comment.setWorkComic(workComic);
                    comment.setWork(workComic.getWork());
                }

                /*UserInfo userInfo = userService.findOneByUserId(comment.getFromUser().getId());
                comment.setUserInfo(userInfo);*/
            }
            Map<String, Object> result = DataTableFactory.fitting(draw, page);
            WebUtil.print(response, result);
        } catch (Exception e) {
            GeneralExceptionHandler.log(e);
            WebUtil.print(response, emptyData);
        }
    }

    /**
     * 批量删除
     */
    @RequestMapping(value = "/deleteBatch", method = RequestMethod.POST)
    @ResponseBody
    public Integer deleteBatch(String ids) {
        Long[] arrayId = JsonUtil.json2Obj(ids, Long[].class);
        for (Long id : arrayId) {
            workCommentService.deleteComment(id);
        }
        return 1;
    }

    /**
     * 新增评论
     */
    @RequestMapping(value = "/addComment")
    @ResponseBody
    public Integer addComment(Long workId, Long userId, String content, Integer type) {

        WorkComment workComment = new WorkComment();
        Work work = workService.queryByPK(workId);
        workComment.setWorkId(workId);
        workComment.setUserInfo(userService.queryByPK(userId));
        workComment.setContent(content);
        workComment.setType(type);
        workComment.setTypePlus(Constant.COMMENT_TYPE_DEFAULT);
        workComment.setIsDetail(Constant.COMMENT_PRAISE_ISDETAIL_NO);
        workComment.setPraise(0);
        workComment.setName(work.getName());
        workCommentService.save(workComment);

        return 1;
    }

    /**
     * 查询主创和后台添加用户列表
     */
    @RequestMapping(value = "/creatorList")
    @ResponseBody
    public List<UserInfo> creatorList() {

        return userService.findList();
    }

    /**
     * 查询对应类型作品列表
     */
    @RequestMapping(value = "/workList")
    @ResponseBody
    public List<Work> workList(Integer type) {

        return workService.findByType(type);
    }

    /**
     * 回复
     */
    @RequestMapping(value = "/replay")
    @ResponseBody
    private Integer replay(Long commentId, Long toUserId, Long fromUserId, String content) {

        WorkComment workComments = workCommentService.queryByPK(commentId);
        WorkComment workComment = new WorkComment();
        workComment.setWorkId(workComments.getWorkId());
        workComment.setName(workComments.getName());
        workComment.setUserInfo(userService.queryByPK(fromUserId));
        workComment.setToUserInfo(userService.queryByPK(toUserId));

        if (workComments.getType() == 0) {
            // 针对评论的回复
            workComment.setWorkCommentId(commentId);
        }else if (workComments.getType() == 1) {
            // 针对回复的回复
            workComment.setWorkCommentId(workComments.getWorkCommentId());
        }
        workComment.setContent(content);
        workComment.setTypePlus(Constant.COMMENT_TYPE_REPLY);
        workComment.setIsDetail(Constant.COMMENT_PRAISE_ISDETAIL_NO);
        workComment.setPraise(0);
        workComment.setType(workComments.getType());
        workComment.setCreateDate(System.currentTimeMillis());
        workCommentService.save(workComment);

        return 1;
    }
}
